﻿using Microsoft.OpenApi.Models;

namespace Simple.WebAi.Extensions
{
    /// <summary>
    /// Swagger扩展
    /// </summary>
    public static class SwaggerExtensions
    {
        /// <summary>
        /// 框架扩展：使用Swagger
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseSwaggerUI(this IApplicationBuilder app)
        {
            var Configuration = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration))!;

            bool IsEnable = bool.Parse(Configuration!["Swagger:IsEnable"]!);
            if (IsEnable)
            {
                app.UseSwagger();

                app.UseSwaggerUI(option =>
                {
                    option.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
                });
            }
            return app;
        }

        /// <summary>
        /// 框架扩展：注册使用Swagger
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddSwaggerGen(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider().GetService<IConfiguration>();

            bool IsEnable = bool.Parse(configuration!["Swagger:IsEnable"]!);
            if (IsEnable)
            {
                string Title = configuration!["Swagger:Title"]!;
                string Description = configuration!["Swagger:Description"]!;
                services.AddSwaggerGen(option =>
                {
                    option.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = Title,
                        Description = $"{Title}<br/>{Description}"
                    });

                    option.AddSecurityRequirement(new OpenApiSecurityRequirement
                    {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                }
                            },
                            new string[] { }
                        }
                    });

                    option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description = "JWT授权(数据将在请求头中进行传输) 在下方输入Bearer {token} 即可，注意两者之间有空格",
                        Name = "Authorization",
                        //jwt默认的参数名称
                        In = ParameterLocation.Header,
                        //jwt默认存放Authorization信息的位置(请求头中)
                        Type = SecuritySchemeType.ApiKey,
                        BearerFormat = "JWT",
                        Scheme = "Bearer",
                    });

                    var XMLFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).Where(x => x.Contains(".xml"));
                    foreach (var XMLFile in XMLFiles)
                    {
                        option.IncludeXmlComments(XMLFile, true);
                    }
                });
            }
            return services;
        }
    }
}
